from openpyxl import Workbook

from openpyxl.chart import (
    PieChart3D,
    Reference
)

# 准备数据
data = [
    ['Pie', 'Sold'],
    ['Apple', 50],
    ['Cherry', 30],
    ['Pumpkin', 10],
    ['Chocolate', 40],
]

# 创建工作表
wb = Workbook()
ws = wb.active

# 向工作表追加数据
for row in data:
    ws.append(row)

# 创建3D饼图
pie = PieChart3D()
# 标签
labels = Reference(ws, min_col=1, min_row=2, max_row=5)
# 数据
data = Reference(ws, min_col=2, min_row=1, max_row=5)
# 饼图添加数据
pie.add_data(data, titles_from_data=True)
# 饼图设置分类
pie.set_categories(labels)
# 饼图设置标题
pie.title = "3D 版饼图"

ws.add_chart(pie, "D1")

wb.save("pie3.xlsx")
